<?php
 /**
  *
  * motionWeb | A webUI for motion.
  * Copyright (c) 2010-2011 Kyle Hotchkiss Productions
  * Licenced under the GPL.
  *
  * cameras.php
  *  View for the cameras page.
  *
  */
  
 if (!defined('motionWeb')) header('Location: /');
 
 include 'app/includes/header.php';
 
 $camerasPage = new cameras();
 $injectFooter = "<a href=\"#\" id=\"addCamera\">add camera</a>";
  
 if ( isset($_POST['addCamera']) ) { 
  if ( $camerasPage->add_camera($_POST['name'], $_POST['location'], $_POST['url'], $_POST['user'], $_POST['pass']) ) {
?>
   <div id="body" class="cameras">
    <div class="title">
     Cameras
    </div>
    <div class="status">
     The camera was successfully added! <a href="">Go back</a>?
    </div>
   </div>
<?php
  } else {
?>
   <div id="body" class="cameras">
    <div class="title">
     Cameras
    </div>
    <div class="status">
     There was an error adding the camera. <a href="">Go back</a>?
    </div>
   </div>
<?php 
  }
 } else if ( isset($_POST['delCamera']) ) {
  if ( $camerasPage->delete_camera($_POST['camID']) ) {
?>
   <div id="body" class="cameras">
    <div class="title">
     Cameras
    </div>
    <div class="status">
     The camera was successfully deleted! <a href="">Go back</a>?
    </div>
   </div>  
<?php
  } else {
?>
   <div id="body" class="cameras">
    <div class="title">
     Cameras
    </div>
    <div class="status">
     You can't delete a non-existant camera! <a href="">Go back</a>?
    </div>
   </div>
<?php
  }
 } else {
?>
   <div id="body" class="cameras">
    <div class="title">
     Cameras
    </div>
<?php 
  $cameras = $camerasPage->list_cameras(); 
   
  if ( $cameras != false ) {
?>
    <div class="list">
<?php
   for ($i = 0; $i < mysql_num_rows($cameras); $i++) {
?>   
    <div class="camera" id="<?php echo mysql_result($cameras, $i, "Camera_id"); ?>">
     <img src="http://192.168.2.9:<?php echo mysql_result($cameras, $i, "Camera_port");?>/" height="240" width="320" />
     <div class="name">
      <?php echo mysql_result($cameras, $i, "Camera_name"); ?>
     </div>
     <div class="location">
      <?php echo mysql_result($cameras, $i, "Camera_location");?>
     </div>
     <div class="actions">
      <div class="edit">
       <form action="" method="post">
       <input type="hidden" name="editCamera" value="true" />
       <input type="hidden" name="camID" value="<?php echo mysql_result($cameras, $i, "Camera_id"); ?>" />
       <input type="submit" value="edit" />
       </form>
      </div>
      <div class="delete">
       <form action="" method="post">
       <input type="hidden" name="delCamera" value="true" />
       <input type="hidden" name="camID" value="<?php echo mysql_result($cameras, $i, "Camera_id"); ?>" />
       <input type="submit" value="delete" />
       </form>
      </div>
     </div>
    </div>
<?php
   }
?> 
   </div>
<?php
  } else {
?>
    <div class="status">
     No cameras have been added yet. <a href="#" id="addCamera">Would you like to add one now</a>?
    </div>
<?php
  }
?>
  </div>
<?php
 }
?>
  </div>
  <div id="overlay">
   <div id="camerasDialogue">
    <div class="background">
    <form action="" method="post">
     <div class="title">
      Add Camera
     </div>
     <p class="description">
      To add a camera, view the relevent documentation for your network camera or copy the information you entered into your streaming software. Only mjpeg streams are supported.
     </p><p>
      <span class="required" title="required">*</span>camera name
      <input type="text" name="name" />
     </p><p>
      <span class="required" title="required">*</span>camera location
      <input type="text" name="location" />
     </p><p>
      <span class="required" title="required">*</span>camera url
      <input type="text" name="url" />
     </p>
     <div class="optional hidden"> 
      <p>
       camera username
       <input type="text" name="user" />
      </p><p>
       camera password
       <input type="text" name="password" />
      </p>
     </div>
     <p>
      <input type="hidden" name="addCamera" value="true" />
      <input type="submit" value="Add Camera" />
     </p>
    </form>
    </div>
   </div>
  </div>
<?php  
 include 'app/includes/footer.php'; 
?>